package com.example.win.android_banduyujia.login.application;

import android.app.NotificationManager;
import android.content.Context;

import com.example.win.android_banduyujia.BanDuApp;
import com.example.win.android_banduyujia.entity.LoginInfEntity;
import com.example.win.android_banduyujia.lib.util.SharedPreferencesUtil;
import com.example.win.android_banduyujia.lib.util.StrUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Win on 2015/8/6.
 */
public class LoginApplication {

    private static LoginApplication mInstance;
    private LoginInfEntity mUser;
    private String   mAccessToken = " ";
    private List<ILoginStatusChangedListener>  mListenerList = new ArrayList<ILoginStatusChangedListener>();
    private LoginInfEntity user;

    public static LoginApplication getInstance(){
        if(mInstance == null){
            mInstance = new LoginApplication();
        }
        return mInstance;
    }

    public void addLoginOrLogoutListener(ILoginStatusChangedListener listener){
        if(!mListenerList.contains(listener))
            mListenerList.add(listener);
    }

    public void removeLoginOrLogoutListener(ILoginStatusChangedListener listener){
        mListenerList.remove(listener);
    }

    public void removeAllListener(){
        mListenerList.clear();
    }

    public void sendLoginListener(){
        if(mListenerList == null || mListenerList.size() == 0)
            return;
        for(ILoginStatusChangedListener listener: mListenerList){
            listener.isLoginOrLogout();
        }
    }

    public void login(LoginInfEntity user) {
        if (user == null)
            return;
        setUser(user);
        setAccessToken(user.getAccessToken());
        SharedPreferencesUtil.saveLoginInfo(user);
        sendLoginListener();
    }

    public boolean conflictlogout(){
         try{
             NotificationManager mNotificationManager = (NotificationManager) BanDuApp.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
             mNotificationManager.cancelAll();
             SharedPreferencesUtil.clearAccessToken();
             mAccessToken = " ";
             // 清除数据库缓存
         }catch (Exception e){
             e.printStackTrace();
         }
        mUser = null;
        sendLoginListener();
        return true;
    }

    public boolean logout(){
        try{
            NotificationManager mNotificationManager = (NotificationManager) BanDuApp.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.cancelAll();
            SharedPreferencesUtil.clearAccessToken();
            mAccessToken = " ";
            // 清除数据库缓存
        }catch (Exception e){
            e.printStackTrace();
        }
        mUser = null;
        sendLoginListener();
        return true;
    }

    public boolean isLogin(){
        return !StrUtils.isEmpty(getAccessToken());
    }

    public String getAccessToken() {
        if(StrUtils.isEmpty(mAccessToken)){
            mAccessToken = SharedPreferencesUtil.getAccessToken();
        }
        return  StrUtils.notNullStr(mAccessToken);
    }

    public LoginInfEntity getUser() {
        return user;
    }

    public void setUser(LoginInfEntity user) {
        this.user = user;
    }

    public void setAccessToken(String mAccessToken){
        this.mAccessToken = mAccessToken;
    }

}
